package com.jiuzhi.app.common.wxutil;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 9786579bc0d54d82958e1c7cdd991d10;4d2f5d68f0cb45c6af6c7977c80313ec;da2683ee80644827804c09b5a7540a20;1cf34dea593211e5b98b00163e000b7c;808e7fcbd9744c0c866135f96a12c0e2
 *
 */
public class InputStreamUtils {

	final static int BUFFER_SIZE = 4096;

	/**
	 * 将InputStream转换成String
	 *
	 * @param in
	 *            InputStream
	 * @return String
	 * @throws Exception
	 *
	 */
	public static String InputStreamTOString(InputStream in) throws Exception {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = in.read()) != -1) {
			baos.write(i);
		}
		in.close();
		return baos.toString();
	}

	/**
	 * 将InputStream转换成某种字符编码的String
	 *
	 * @param in
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static String InputStreamTOString(InputStream in, String encoding) throws Exception {

		StringBuffer sb = new StringBuffer();
		byte[] b = new byte[1024];
		int len = 0;
		try {
			if (encoding == null || encoding.equals("")) {
				// 默认以utf-8形式
				encoding = "utf-8";
			}
			while ((len = in.read(b)) != -1) {
				sb.append(new String(b, 0, len, encoding));
			}
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}

	/**
	 * 将String转换成InputStream
	 *
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static InputStream StringTOInputStream(String in) throws Exception {

		ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
		return is;
	}

	/**
	 * 将InputStream转换成byte数组
	 *
	 * @param in
	 *            InputStream
	 * @return byte[]
	 * @throws IOException
	 */
	public static byte[] InputStreamTOByte(InputStream in) throws IOException {

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int count = -1;
		while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
			outStream.write(data, 0, count);

		data = null;
		return outStream.toByteArray();
	}

	/**
	 * 将byte数组转换成InputStream
	 *
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static InputStream byteTOInputStream(byte[] in) throws Exception {

		ByteArrayInputStream is = new ByteArrayInputStream(in);
		return is;
	}

	/**
	 * 将byte数组转换成String
	 *
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static String byteTOString(byte[] in) throws Exception {

		InputStream is = byteTOInputStream(in);
		return InputStreamTOString(is);
	}

	/**
	 * 解析返回XML数据
	 *
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> getWeChatPay(String xmlString) throws Exception {
		Map<String, String> retMap = new HashMap<String, String>();
		// 创建一个新的字符串
		StringReader read = new StringReader(xmlString);
		// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
		InputSource source = new InputSource(read);
		SAXBuilder sax = new SAXBuilder();
		Document doc;
		doc = sax.build(source);
		Element root = doc.getRootElement();
		if (null != root) {
			Iterator<?> it = root.getChildren().iterator();
			while (it.hasNext()) {
				Element elm = (Element) it.next();
				System.out.println(elm.getName() + "---" + elm.getTextTrim());
				retMap.put(elm.getName(), elm.getTextTrim());
			}

		}

		return retMap;
	}

}
